package com.ruoyi.pay.config;

import com.alipay.api.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;

@Configuration
@PropertySource("classpath:alipay-sandbox.properties")  //加载配置文件
@Slf4j
public class AliPayClientConfig {

    // 依赖注入
    @Resource
    private Environment environment;

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        // 支付宝网关
        alipayConfig.setServerUrl(environment.getProperty("alipay.gateway-url"));
        // APPID
        alipayConfig.setAppId(environment.getProperty("alipay.app-id"));
        // 支付宝公钥
        alipayConfig.setAlipayPublicKey(environment.getProperty("alipay.alipay-public-key"));
        // 应用私钥
        alipayConfig.setPrivateKey(environment.getProperty("alipay.merchant-private-key"));
        // 请求格式 JSON
        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
        // 设置字符集
        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
        // 设置签名类型
        alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);

        // 构造
        return new DefaultAlipayClient(alipayConfig);
    }

}
